package com.xuelang.pipeline.entry.application;

import com.xuelang.pipeline.app.application.ComponentService;
import com.xuelang.pipeline.client.application.IComponentService;
import com.xuelang.pipeline.client.application.dto.ComponentDTO;
import com.xuelang.pipeline.client.application.dto.ComponentQuery;
import com.xuelang.pipeline.client.application.dto.ComponentTaskQuery;
import com.xuelang.pipeline.client.common.constant.DictDTO;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.ApiResult;
import com.xuelang.pipeline.client.common.result.PageRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 组件服务web控制类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@RestController
@RequestMapping("/component")
public class ComponentController implements IComponentService {


    @Autowired
    private ComponentService componentService;

    @Override
    public ApiResult<PageRecord<ComponentDTO>> pageComponentList(PageParam<ComponentQuery> dtoPageParam) {
        return ApiResult.success(componentService.pageList(dtoPageParam));
    }

    @Override
    public ApiResult<List<ComponentDTO>> conditionalComponentList(ComponentQuery componentQuery) {
        return ApiResult.success(componentService.list(componentQuery));
    }

    @Override
    public ApiResult<List<ComponentDTO>> listComponentByKeyword(String keyword) {
        return ApiResult.success(componentService.list(ComponentQuery.builder().name(keyword).build()));
    }

    @Override
    public ApiResult<Boolean> addComponent(ComponentDTO componentDTO) {
        return ApiResult.success(componentService.add(componentDTO));
    }

    @Override
    public ApiResult<Boolean> updateComponent(ComponentDTO componentDTO) {
        return ApiResult.success(componentService.update(componentDTO));
    }

    @Override
    public ApiResult<Boolean> deleteComponent(String componentLabel) {
        return ApiResult.success(componentService.delete(componentLabel));
    }

    @Override
    public ApiResult<ComponentDTO> queryComponent(String componentLabel) {
        return ApiResult.success(componentService.query(componentLabel));
    }

    @Override
    public ApiResult<List<DictDTO>> queryRepoDictByComponentId(ComponentTaskQuery query) {
        return ApiResult.success(componentService.queryComponentRepo(query.getComponentLabel()));
    }

    @Override
    public ApiResult<List<DictDTO>> queryTokenDictByComponentId(ComponentTaskQuery query) {
        return ApiResult.success(componentService.queryComponentToken(query.getComponentLabel()));
    }

    @Override
    public ApiResult<ComponentDTO> queryByid(String id) {
        return ApiResult.success(componentService.queryById(Long.parseLong(id)));
    }
}